It shows how to configure camera event features, register an event callback, and process the events acquired from the event callback. Event example: ExposureEnd.
12 currentsystem = platform.system()
13 if currentsystem ==
'Windows':
14 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
16 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
17 from MvCameraControl_class
import *
20 if sys.version_info[0] < 3:
22 input_func = raw_input
30 Safely decode a string from a ctypes character array. 31 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 33 byte_str = memoryview(ctypes_char_array).tobytes()
36 null_index = byte_str.find(b
'\x00')
38 byte_str = byte_str[:null_index]
41 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
43 return byte_str.decode(encoding)
44 except UnicodeDecodeError:
48 return byte_str.decode(
'latin-1', errors=
'replace')
52 stEventInfo = POINTER(MV_EVENT_OUT_INFO)
53 EventInfoCallBack =
fun_ctype(
None, stEventInfo, c_void_p)
56 stPEventInfo = cast(pEventInfo, POINTER(MV_EVENT_OUT_INFO)).contents
57 nBlockId = stPEventInfo.nBlockIdHigh
58 nBlockId = (nBlockId << 32) + stPEventInfo.nBlockIdLow
59 nTimestamp = stPEventInfo.nTimestampHigh
60 nTimestamp = (nTimestamp << 32) + stPEventInfo.nTimestampLow
62 print (
"EventName[%s], EventId[%u], BlockId[%d], Timestamp[%d]" % (stPEventInfo.EventName.decode(
'UTF-8'),
63 stPEventInfo.nEventID, nBlockId, nTimestamp))
67 if __name__ ==
"__main__":
71 MvCamera.MV_CC_Initialize()
73 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
74 print (
"SDKVersion[0x%x]" % SDKVersion)
76 deviceList = MV_CC_DEVICE_INFO_LIST()
77 tlayerType = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
78 | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
81 ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
83 print (
"enum devices fail! ret[0x%x]" % ret)
86 if deviceList.nDeviceNum == 0:
87 print (
"find no device!")
90 print (
"find %d devices!" % deviceList.nDeviceNum)
92 for i
in range(0, deviceList.nDeviceNum):
93 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
94 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
95 print (
"\ngige device: [%d]" % i)
96 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
97 print (
"device model name: %s" % strModeName)
99 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
100 print(
"device serial number: %s" % strSerialNumber)
102 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
103 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
104 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
105 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
106 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
107 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
108 print (
"\nu3v device: [%d]" % i)
109 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
110 print (
"device model name: %s" % strModeName)
112 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
113 print (
"device serial number: %s" % strSerialNumber)
114 elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
115 print (
"\nCML device: [%d]" % i)
116 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
117 print (
"device model name: %s" % strModeName)
119 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
120 print (
"device serial number: %s" % strSerialNumber)
121 elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
122 print (
"\nCXP device: [%d]" % i)
123 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
124 print (
"device model name: %s" % strModeName)
126 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
127 print (
"device serial number: %s" % strSerialNumber)
128 elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
129 print (
"\nXoF device: [%d]" % i)
130 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
131 print (
"device model name: %s" % strModeName)
133 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
134 print (
"device serial number: %s" % strSerialNumber)
136 nConnectionNum =
input_func(
"please input the number of the device to connect:")
138 if int(nConnectionNum) >= deviceList.nDeviceNum:
139 print (
"intput error!")
146 stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
148 ret = cam.MV_CC_CreateHandle(stDeviceList)
150 raise Exception (
"create handle fail! ret[0x%x]" % ret)
153 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
155 raise Exception (
"open device fail! ret[0x%x]" % ret)
158 if stDeviceList.nTLayerType == MV_GIGE_DEVICE
or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
159 nPacketSize = cam.MV_CC_GetOptimalPacketSize()
160 if int(nPacketSize) > 0:
161 ret = cam.MV_CC_SetIntValue(
"GevSCPSPacketSize",nPacketSize)
163 print (
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
165 print (
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
168 ret = cam.MV_CC_SetEnumValue(
"TriggerMode", MV_TRIGGER_MODE_OFF)
170 raise Exception (
"set trigger mode fail! ret[0x%x]" % ret)
173 ret = cam.MV_CC_EventNotificationOn(
"ExposureEnd")
175 raise Exception (
"Set Event Notification On fail! ret[0x%x]" % ret)
178 ret = cam.MV_CC_RegisterEventCallBackEx(
"ExposureEnd", CALL_BACK_FUN,
None)
180 raise Exception (
"register event callback fail! ret [0x%x]" % ret)
183 ret = cam.MV_CC_StartGrabbing()
185 raise Exception (
"start grabbing fail! ret[0x%x]" % ret)
187 print (
"press Enter key to stop grabbing.")
191 ret = cam.MV_CC_StopGrabbing()
193 raise Exception (
"stop grabbing fail! ret[0x%x]" % ret)
196 ret = cam.MV_CC_CloseDevice()
198 raise Exception (
"close deivce fail! ret[0x%x]" % ret)
201 cam.MV_CC_DestroyHandle()
203 except Exception
as e:
205 cam.MV_CC_CloseDevice()
206 cam.MV_CC_DestroyHandle()
209 MvCamera.MV_CC_Finalize()